# K8s安装脚本
set -e

K8S_FOLDER="/home/lorry/cn_shell"

if [ $# -eq 0 ];
then
    echo "please point the role 'master' or 'worker'"
    exit
fi
if [ "$1" != "master" ] && [ "$1" != "worker" ]
then
    echo "parameter can only be 'master' or 'worker'"
    exit
else
    echo "deploy ROLE is: $1"
fi
echo ""
echo "=========================="
echo "0. config sourcelist"
echo "=========================="
echo ""
mv /etc/apt/sources.list /etc/apt/sources.list.bak
# cp ${K8S_FOLDER}/sources.list /etc/apt
cat << EOF | tee /etc/apt/sources.list
deb http://bg9:8081/repository/ubunt_2004_apt/ focal main restricted universe multiverse
deb http://bg9:8081/repository/ubunt_2004_apt/ focal-security main restricted universe multiverse
deb http://bg9:8081/repository/ubunt_2004_apt/ focal-updates main restricted universe multiverse
deb http://bg9:8081/repository/ubunt_2004_apt/ focal-backports main restricted universe multiverse
EOF
# 为k8s的安装，添加阿里源
# sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - 
# echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" >/etc/apt/sources.list.d/kubernetes.list

# 为k8s添加Nexus本地代理源
echo "deb http://bg9:8081/repository/ubunt_1604_apt kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
apt update

echo ""
echo "=========================="
echo "1. docker && install cri-docker"
echo "=========================="
echo ""
apt install docker.io -y
cat << EOF | tee /etc/docker/daemon.json
{
    "registry-mirrors": ["https://50ogchrt.mirror.aliyuncs.com"],
    "insecure-registries":["bg9:8082", "bg9:8083", "bg9:8084"]
}
EOF
systemctl daemon-reload
systemctl restart docker

apt install $K8S_FOLDER/software/cri-dockerd_0.3.0.3-0.ubuntu-jammy_amd64.deb -y
# 为启动cri-docker时添加启动参数，指定cni信息以及pause镜像信息
cp $K8S_FOLDER/cri-docker.service /lib/systemd/system/
# 切记要重现加载daemon文件以及重启cri-docker
systemctl daemon-reload && systemctl restart cri-docker

echo ""
echo "=========================="
echo "2. install kube*..."
echo "=========================="
echo ""
sudo apt install -y apt-transport-https curl
# 安装三大件 + cni
sudo apt install -y kubelet kubeadm kubectl kubernetes-cni

echo ""
echo "=========================="
echo "3. config ip forward..."
echo "=========================="
echo ""

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1 # better than modify /etc/sysctl.conf
EOF

sudo sysctl --system

echo ""
echo "=========================="
echo "4. off swapping... ..."
echo "=========================="
echo ""

sudo swapoff -a
sudo sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab

echo ""
echo "=========================="
echo "5. lock version..."
echo "=========================="
echo ""

sudo apt-mark hold kubeadm kubelet kubectl

echo ""
echo "=========================="
echo "6. pull images..."
echo "=========================="
echo ""

# kubeadm_config=/root/kubeadmcfg.yaml
kubeadm_config=/home/lorry/cn_shell/kubeadm.yml
echo "kubeadm_config path: $kubeadm_config"
echo "list the images which wil be docker pulled"
kubeadm config images list --config $kubeadm_config

echo "pull the images..."
kubeadm config images pull --config $kubeadm_config

echo "reset k8s env..."
kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock -f --v=9
rm -rf /etc/cni/net.d
rm -rf $HOME/.kube/config
# ipvsadm --clear
systemctl daemon-reload
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X


if [ "$1" = "master" ]; then
    echo ""
    echo "=========================="
    echo "6.1. Init K8s master..."
    echo "=========================="
    echo ""

    sleep 5s
    kubeadm init --config $kubeadm_config --v=9
	
	echo ""
    echo "================================="
    echo "6.2. copy config file to $HOME..."
    echo "================================="
    echo ""
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config


    echo ""
    echo "=========================="
    echo "7. Install Calico..."
    echo "=========================="
    echo ""
    kubectl apply -f $K8S_FOLDER/calico.yaml
fi


echo ""
echo "=========================="
echo "8. Test Get node..."
echo "=========================="
echo ""
sleep 3s
kubectl get node

# last remind the user to join the master
if [ "$1" = "worker" ]; then
    echo ""
    echo "=========================="
    echo "worker: execute below command on the MASTER node for get join command:"
    echo "sudo kubeadm token create --print-join-command"
    echo "=========================="
    echo ""
fi